package com.zhehekeji.opsys.api.service.validate;

import org.apache.cxf.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zhehekeji.opsys.api.code.UserRegisterResultCode;
import com.zhehekeji.opsys.api.common.util.SuperResp;
import com.zhehekeji.opsys.api.entity.TUser;
import com.zhehekeji.opsys.api.service.validate.args.UserRegisterArgs;

/**
 * @ClassName UserRegisterValidate
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2015-4-14   下午2:31:04
 */
@Service
public class UserRegisterValidate extends ValidateService<ValidateRets<TUser>, UserRegisterArgs> {
	
	private final static Logger logger = LoggerFactory.getLogger(UserRegisterValidate.class);
	
	@Override
	public ValidateRets<TUser> validate(UserRegisterArgs validateArgs)
			throws Exception {
		String tmpAccount = validateArgs.getAccount();
		//String tmpMobile = validateArgs.getMobile();
		Long tmpCpCode = validateArgs.getCpCode();
		SuperResp resp = validateArgs.getResp();
		ValidateRets<TUser> validateRets = new ValidateRets<TUser>();
		logger.info("账号:"+tmpAccount+",CpCode:"+tmpCpCode);
		if (StringUtils.isEmpty(tmpAccount)){
			logger.error("注册失败，账号非法！");
			resp.setResultCode(UserRegisterResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("注册失败，账号非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		/*if (StringUtils.isEmpty(tmpMobile)){
			logger.error("注册失败，手机号非法！");
			resp.setResultCode(UserRegisterResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("注册失败，手机号非法！");
			validateRets.setSuccess(false);
			return validateRets;
		}*/
//		if (tmpCpCode == null || tmpCpCode <= 0){
//			logger.error("注册失败，cpCode非法！");
//			resp.setResultCode(UserRegisterResultCode.PARAMETER_IS_NOT_LEGAL);
//			resp.setResultDesc("注册失败，cpCode非法！");
//			validateRets.setSuccess(false);
//			return validateRets;
//		}
//		TCompany tmpTCompany = entityDao.get(TCompany.class, "o.code=?1", new Object[]{tmpCpCode});
//		if (tmpTCompany == null){
//			logger.error("注册失败，企业不存在，cpCode非法！");
//			resp.setResultCode(UserRegisterResultCode.PARAMETER_IS_NOT_LEGAL);
//			resp.setResultDesc("注册失败，企业不存在，cpCode非法！");
//			validateRets.setSuccess(false);
//			return validateRets;
//		}
//		Integer tmpStatus = tmpTCompany.getStatus();
//		if (tmpStatus == 0){
//			logger.error("注册失败，企业已经停用！");
//			resp.setResultCode(UserRegisterResultCode.COMPANY_HAS_BEEN_STOPPED);
//			resp.setResultDesc("注册失败，企业已经停用！");
//			validateRets.setSuccess(false);
//			return validateRets;
//		}
		TUser tmpTUser = entityDao.get(TUser.class, "o.account=?1", new Object[]{tmpAccount});
		if (tmpTUser != null){
			logger.error("注册失败，用户已存在！");
			resp.setResultCode(UserRegisterResultCode.USER_ALREADY_EXISTS);
			resp.setResultDesc("注册失败，用户已存在！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		/*tmpTUser = entityDao.get(TUser.class, "o.mobile=?1 and o.cpCode=?2", new Object[]{tmpMobile, tmpCpCode});
		if (tmpTUser != null){
			logger.error("注册失败，手机已绑定其他账号！");
			resp.setResultCode(UserRegisterResultCode.MOBILE_PHONES_HAS_BINDING);
			resp.setResultDesc("注册失败，手机已绑定其他账号！");
			validateRets.setSuccess(false);
			return validateRets;
		}*/
		
		return validateRets;
		
	}

}
